package com.cloudsigns.factorymanager2.engineering.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class MediaListByRegionIdModel implements IMediaListByRegionIdModel {

    @Override
    public void getMediaListById(final String id, final CommonEnum.EngineeringORderCategory engineeringDeptOrderCategoryEnum, final OnMediaListByIdListener mediaListByIdListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                String url = ProjectUtil.MEDIA_LIST_BY_ID_URL + "?engineeringDeptOrderCategoryEnum=" + engineeringDeptOrderCategoryEnum + "&id="+ id;
//                String url = ProjectUtil.MEDIA_LIST_BY_ID_URL + "?id=" + id;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != mediaListByIdListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("mediaList"));
                            List<MediaBean> mediaRegionBeanList = new ArrayList<MediaBean>();
                            if (jsonArr != null) {

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        MediaBean mediaRegionBean = new MediaBean();
                                        mediaRegionBean.setAreaName(jsonObj.getString("areaName"));
                                        mediaRegionBean.setCompanyId(jsonObj.getString("companyId"));
                                        mediaRegionBean.setId(jsonObj.getString("id"));
                                        mediaRegionBean.setMediaName(jsonObj.getString("mediaName"));
                                        mediaRegionBean.setNumber(jsonObj.getString("number"));

//                                        String positionXStr = jsonObj.getString("positionX");
//                                        if (null != positionXStr && !"null".equals(positionXStr)) {
//                                            double positionX = Double.parseDouble(positionXStr);
//                                            mediaRegionBean.setPositionX(positionX);
//                                        }
//
//                                        String positionYStr = jsonObj.getString("positionY");
//                                        if (null != positionYStr && !"null".equals(positionYStr)) {
//                                            double positionY = Double.parseDouble(positionYStr);
//                                            mediaRegionBean.setPositionY(positionY);
//                                        }


                                        mediaRegionBean.setRegionId(jsonObj.getString("regionId"));
                                        mediaRegionBean.setRegionName(jsonObj.getString("regionName"));
                                        mediaRegionBean.setEnable(jsonObj.getBoolean("enable"));

                                        mediaRegionBeanList.add(mediaRegionBean);

                                    }
                                }

                                mediaListByIdListener.getMediaListByIdSuccessed(mediaRegionBeanList);

                            }


                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            mediaListByIdListener.getMediaListByIdFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnMediaListByIdListener {
        void getMediaListByIdSuccessed(List<MediaBean> mediaRegionBeanList);

        void getMediaListByIdFailed(String desc);
    }
}
